package character;

/**
 * The social links for characters.
 * @author pcdejager
 *
 */
public class SocialLinks {
    /**
     * Constructor.
     */
    public SocialLinks() {
    }

    /**
     * Get the love interest.
     * @return The love interest
     */
    public final Character getLoveInterest() {
        return loveInterest;
    }

    /**
     * Get the spouse.
     * @return The spouse
     */
    public final Character getSpouse() {
        return spouse;
    }

    /**
     * Does the character have a love interest.
     * @return True if has love interest, else false
     */
    public final boolean hasLoveInterest() {
        return (loveInterest != null);
    }

    /**
     * Is the character married.
     * @return True if married, else false
     */
    public final boolean isMarried() {
        return (spouse != null);
    }

    /**
     * The character fell in love.
     * @param c The love interest
     * @return True if ok, else false
     */
    public final boolean fallInLove(final Character c) {
        if (c == null) {
            return false;
        }
        if (loveInterest != null) {
            return false;
        }
        loveInterest = c;
        return true;
    }

    /**
     * The character married.
     * @param c The spouse
     * @return True if ok, else false
     */
    public final boolean marry(final Character c) {
        if (c == null) {
            return false;
        }
        if (spouse != null) {
            return false;
        }
        spouse = c;
        return true;
    }

    /**
     * Character died, remove links.
     * @param c The character that died
     */
    public final void died(final Character c) {
        if (loveInterest == c) {
            loveInterest = null;
        }
        if (spouse == c) {
            spouse = null;
        }
        // TODO : Log/Keep history?
    }

    /**
     * Make a connection by falling in love.
     * @param c1 The first character
     * @param c2 The second character
     * @return True if ok, else false.
     */
    public static boolean fallInLove(
            final Character c1,
            final Character c2) {
        if ((c1 == null) || (c2 == null)) {
            return false;
        }
        if ((c1.getSocialLinks().getLoveInterest() != null)
                || (c2.getSocialLinks().getLoveInterest() != null)) {
            return false;
        }
        return c1.getSocialLinks().fallInLove(c2)
                && c2.getSocialLinks().fallInLove(c1);
    }

    /**
     * Make a connection by marrying.
     * @param c1 The first character
     * @param c2 The second character
     * @return True if ok, else false
     */
    public static boolean marry(final Character c1, final Character c2) {
        if ((c1 == null) || (c2 == null)) {
            return false;
        }
        if ((c1.getSocialLinks().getSpouse() != null)
                || (c2.getSocialLinks().getSpouse() != null)) {
            return false;
        }
        return c1.getSocialLinks().marry(c2)
                && c2.getSocialLinks().marry(c1);
    }

    /**
     * The love interest.
     */
    private Character loveInterest = null;
    /**
     * The spouse.
     */
    private Character spouse = null;
}
